//
//  TVCommodityGiftTipsView.swift
//  iPhone
//
//  Created by Toj on 8/15/22.
//

import UIKit

class TVCommodityGiftTipsView: UIView {
    
    var cellModel: TVCommodityCellModel? { didSet {
        guard let model = cellModel else { return }
        tipsLabel.text = model.discountDescription
        isHidden = !model.isDiscount
    } }
    
    @objc private func didTapMore() {
        STAlertHelper.manager.show(message: tipsLabel.text ?? "")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        setupMakeLayoutSubviews()
    }
    
    // 提示背景
    private lazy var tipsView: UIView = {
        let view = UIView()
        view.st_cornerRadius = 4
        view.backgroundColor = "#FFFAEE".to_color
        view.st_border(color: "#FFF6D6".to_color)
        return view
    }()
    
    // 礼物图标
    private lazy var giftIcon: UIImageView = {
       let view = UIImageView()
        view.image = "commodity_gift".to_image
        return view
    }()
    
    // 提示
    private lazy var tipsLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#99773C".to_color
        view.font = UIFont.systemFont(ofSize: 12)
        return view
    }()
    
    // 更多图标
    private lazy var moreIcon: UIButton = {
       let view = UIButton()
        view.setImage("commodity_tv_more".to_image, for: .normal)
        view.setImage("commodity_tv_more".to_image, for: .highlighted)
        view.addTarget(self, action: #selector(didTapMore), for: .touchUpInside)
        return view
    }()
}

// MARK: - 初始化
extension TVCommodityGiftTipsView {
    
    private func setupMakeAddSubviews() {
        
        addSubview(tipsView)
        addSubview(giftIcon)
        addSubview(tipsLabel)
        addSubview(moreIcon)
    }
    
    private func setupMakeLayoutSubviews() {
        
        tipsView.snp.makeConstraints { make in
            make.edges.equalTo(self)
        }
        
        giftIcon.snp.makeConstraints { make in
            make.centerY.equalTo(self)
            make.left.equalTo(8)
            make.width.equalTo(20)
            make.height.equalTo(20)
        }
        
        moreIcon.snp.makeConstraints { make in
            make.centerY.equalTo(self)
            make.right.equalTo(snp.right).offset(-8)
            make.width.equalTo(20)
            make.height.equalTo(20)
        }
        
        tipsLabel.snp.makeConstraints { make in
            make.centerY.equalTo(self)
            make.left.equalTo(giftIcon.snp.right).offset(8)
            make.right.equalTo(moreIcon.snp.left).offset(-8)
        }
    }
}
